package com.sl.au.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.sl.au.entity.EventReport;
import com.sl.au.entity.User;
import com.sl.au.service.EventReportService;

/**
 * 上报事件回馈
 * @author yangdi
 *
 */
@RestController
@RequestMapping(value = "/ui/eventfeedback") 
public class EventFeedbackController  extends BaseController{
	
	@Autowired
	private EventReportService eventReportService;

	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index(ModelAndView modelAndView,HttpServletRequest request) {
		String device = request.getParameter("device");
		String identify = request.getParameter("identify");
		if(StringUtils.hasText(device) && device.equals("mobile")) {
			modelAndView.setViewName("mobile/event/eventFeedback");
			modelAndView.addObject("identify", identify);
		}else {
			modelAndView = this.createLayoutView("");
		}
		return modelAndView;
	}
	
	@RequestMapping(value="getListView",method = RequestMethod.GET)
	public Map<String, Object> getListView(){
		Map<String, Object> map = new HashMap<String, Object>();
		User user = this.getMyAccount().getUser();
		
		List<EventReport> list = new ArrayList<EventReport>();
		
		list = eventReportService.getEventReportByUser(0, Integer.MAX_VALUE, user, "createTime", Sort.Direction.DESC);
		map.put("list", list);
		return map;
	}
	
	@RequestMapping(value="findEventReport/{id}",method = RequestMethod.GET)
	public Map<String, Object> findEventReport(@PathVariable("id") String id){
		Map<String, Object> map = new HashMap<String, Object>();
		EventReport eventReport = eventReportService.getRepository().findOne(id);
		map.put("eventReport", eventReport);
		return map;
	}
	
}
